//forever start -l forever.log -o out.log -e err.log www/run.js -baseport 8360 -count 8 -basealias game 

var option = {}
for (var i = 0; i < process.argv.length; i = i + 2) {
    if (process.argv[i] == '-l') {
        var v = process.argv[i + 1]
        option.link = (v)
    }
    if (process.argv[i] == '-c') {
        var v = process.argv[i + 1]
        option.count = v
    }
    if (process.argv[i] == '-n') {
        var v = process.argv[i + 1]
        option.name = v
    }
    if (process.argv[i] == '-p') {
        var v = process.argv[i + 1]
        option.ports = v.split("|")
    }

}
if (option.link == undefined) {
    console.log('link is required : -l 127.0.0.1')
    process.exit()
}
if (option.count == undefined) {
    console.log('rcount is required : -c count')
    process.exit()
}
if (option.name == undefined) {
    console.log('name is required : -n name')
    process.exit()
}
if (option.ports == undefined) {
    console.log('ports is required : -p 8080|8081')
    process.exit()
}
const spawn = require('child_process').spawn
const exec = require('child_process').exec
const fork = require('child_process').fork
const OS = require('os')
const cpuCount = OS.cpus().length
const platform = OS.platform()
var path = require('path')
var rootPath = __dirname
var fs = require('fs')

console.log(rootPath)

function startSpwan(link, name, index) {
    var _link = link
    var _name = name
    var _index = index
    console.log('start')
    var n = spawn('node', ['run.js', '-n', name, '-l', link], {
        cwd: rootPath
    })

    console.log('end')
    n.stdout.on('data', (data) => {
        console.log(n.pid + '-' + _name + `: stdout: ${data}`)
        //	let date = new Date()
        //	fs.appendFile(date.getHours() + "_" + _port + "log.txt", n.pid + "-" + _alias + "-" + _port + `: stdout: ${data}`, function () {})
    })

    n.stderr.on('data', (data) => {
        console.error(n.pid + '-' + _name + `: stderr: ${data}`)
        let date = new Date()
        fs.appendFile(date.getHours() + '_' + _name + 'err.txt', n.pid + '-' + _name + `: stdout: ${data}`, function () {})
    })
    n.on('close', function (code) {
        startSpwan(_link, _name, _index)
    })

    if (platform == 'win32') {
        var ls = spawn('SetProcessAffinityMask.exe', ['-pid', n.pid, '-cpu', index % cpuCount + 1])
        ls.stdout.on('data', (data) => {
            console.log('SetProcessAffinityMask:' + n.pid + `: stdout: ${data}`)
        })

        ls.stderr.on('data', (data) => {
            console.log('SetProcessAffinityMask:' + n.pid + `: stdout: ${data}`)
        })
        ls.on('close', function (code) {
            console.log('SetProcessAffinityMask:exitcode=' + code)
        })
    } else {
        var ls = spawn('taskset', ['-pc', index % cpuCount, n.pid])
        ls.stdout.on('data', (data) => {
            console.log('taskset:' + n.pid + `: stdout: ${data}`)
        })

        ls.stderr.on('data', (data) => {
            console.log('taskset:' + n.pid + `: stdout: ${data}`)
        })
        ls.on('close', function (code) {
            console.log('taskset:exitcode=' + code)
        })
    }
}
console.log(option.link);
for (var i = 0; i < option.count; i++) {
    startSpwan(option.link + ":" + option.ports[i % option.ports.length], option.name + ':' + (i), i)
}